package cn.zhangxin.pethome.system.shop.service.impl;

import cn.zhangxin.pethome.constants.BaseConstants;
import cn.zhangxin.pethome.jwt.LoginContextUtils;
import cn.zhangxin.pethome.system.employee.domain.Employee;
import cn.zhangxin.pethome.system.employee.domain.EmployeeBo;
import cn.zhangxin.pethome.system.employee.mapper.EmployeeMapper;
import cn.zhangxin.pethome.system.shop.domain.Shop;
import cn.zhangxin.pethome.system.shop.domain.ShopAuditLog;
import cn.zhangxin.pethome.system.shop.mapper.ShopAuditLogMapper;
import cn.zhangxin.pethome.system.shop.mapper.ShopMapper;
import cn.zhangxin.pethome.system.shop.service.IShopAuditLogService;
import cn.zhangxin.pethome.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.mail.SimpleMailMessage;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zx
 * @since 2023-01-09
 */
@Service
public class ShopAuditLogServiceImpl extends BaseServiceImpl<ShopAuditLog> implements IShopAuditLogService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${sentEmail}")
    private String sentEmail;

    @Override
    public void add(ShopAuditLog shopAuditLog, Integer state, HttpServletRequest request) {
        shopAuditLog.setState(state);//添加状态
        shopAuditLog.setAuditTime(new Date());//添加时间
        Employee employee1 = (Employee) LoginContextUtils.getLoginUser(request);
        Long id = employee1.getId();
        shopAuditLog.setAuditId(id);
        /*
         * 修改店铺状态
         */
        Shop shop = shopMapper.findOne(shopAuditLog.getShopId());//通过shopid查询shop
        shop.setState(state);//给账号设置状态
        shopMapper.update(shop);//修改店铺


        /*
         * 修改员工状态
            如果激活成功 设置店铺管理员（员工）状态
         */
        if(state==BaseConstants.Shop.STATE_ACTIVE_SUCCESS){
            Long adminId = shop.getAdminId();//获得员工id
            Employee employee = employeeMapper.findOne(adminId);//通过id查询员工
            employee.setState(BaseConstants.Employee.STATE_NORMAL);//设置员工的状态
            employeeMapper.update(employee);//修改员工状态
        }


        /*
            添加日志
         */
        shopAuditLogMapper.add(shopAuditLog);

        /*
            发送邮件
         */
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //如果通过则发送短线
        if (state == BaseConstants.Shop.STATE_WAIT_ACTIVE){
            //发送邮件
            mailMessage.setFrom(sentEmail);
            //邮件主题
            mailMessage.setSubject("宠物之家商家入驻确认");
            //邮件内容
            mailMessage.setText("<a herf=http://127.0.0.1:8080/shopAuditLog/"+shopAuditLog.getShopId()+">"+"点击激活"+"</a>");
            //收件人
            //获取商家管理员email
            EmployeeBo employeeBo = employeeMapper.findOneBo(shop.getAdminId());
            mailMessage.setTo(employeeBo.getEmail());
            javaMailSender.send(mailMessage);
        }
        /*
        驳回发送邮件
         */
        if (state == BaseConstants.Shop.STATE_REJECT_AUDIT){
            mailMessage.setFrom(sentEmail);
            //邮件主题
            mailMessage.setSubject("宠物之家商家入驻驳回");
            //邮件内容
            if(shopAuditLog.getNote()==null || !"".equals(shopAuditLog.getNote().trim())){
                mailMessage.setText("被驳回，请重新入驻!");
            } else {
                mailMessage.setText(shopAuditLog.getNote());
            }
            //收件人
            //获取商家管理员email
            EmployeeBo employeeBo = employeeMapper.findOneBo(shop.getAdminId());
            mailMessage.setTo(employeeBo.getEmail());
            javaMailSender.send(mailMessage);
        }


    }
}
